<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GPT聊天界面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 600px;
            margin: 20px auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .chat-message {
            margin-bottom: 20px;
        }
        .chat-message .user {
            text-align: right;
            color: #0084ff;
        }
        .chat-message .gpt {
            text-align: left;
            color: #666;
        }
        .chat-input {
            display: flex;
            margin-top: 20px;
        }
        .chat-input input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
        }
        .chat-input button {
            padding: 10px 20px;
            background-color: #0084ff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .chat-input button:hover {
            background-color: #007bff;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="chat-messages">
        <!-- 聊天内容 -->
        <div class="chat-message">

        </div>
        <div class="chat-message">

        </div>
    </div>
    <div class="chat-input">
        <input type="text" id="chat-input" placeholder="请输入消息...">
        <button onclick="sendMessage()">发送</button>
    </div>
</div>

<script>
    function sendMessage() {
        var input = document.getElementById('chat-input');
        var message = input.value;
        if (message.trim() === '') {
            return;
        }

        // 添加用户消息到聊天界面
        var chatMessages = document.querySelector('.chat-messages');
        var userMessageDiv = document.createElement('div');
        userMessageDiv.className = 'chat-message';
        userMessageDiv.innerHTML = '<div class="user">' + message + '</div>';
        chatMessages.appendChild(userMessageDiv);

        // 清空输入框
        input.value = '';

        // 创建一个空的 GPT 消息容器
        var gptMessageDiv = document.createElement('div');
        gptMessageDiv.className = 'chat-message';
        var gptContentDiv = document.createElement('div');
        gptContentDiv.className = 'gpt';
        gptMessageDiv.appendChild(gptContentDiv);
        chatMessages.appendChild(gptMessageDiv);

        // 初始化 EventSource 连接
        var eventSource = new EventSource('/api/v1/chat/store?message=' + encodeURIComponent(message));

        // 监听数据流
        eventSource.onmessage = function(event) {
            // 解析 JSON 数据
            var data = JSON.parse(event.data);

            // 检查数据类型，确保是 'data' 类型
            if (data.result) {
                // 将数据片段添加到 GPT 消息容器中
                gptContentDiv.innerHTML += data.result.output.content;
                // 滚动到聊天窗口底部
                chatMessages.scrollTop = chatMessages.scrollHeight;
            }

            // 如果后端发送了结束信号，关闭 EventSource
            if (data.result.metadata.finishReason) {
                eventSource.close();
            }
        };

        // 监听错误
        eventSource.onerror = function(event) {
            console.error('EventSource failed:', event);
            eventSource.close();
        };
    }

</script>
</body>
</html>
